package com.atguigu.base;

import com.atguigu.util.CastUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.management.relation.Role;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

public abstract class BaseServiceImpl<T> implements BaseService<T> {

    protected abstract BaseDao<T> getEntityDao();

    @Override
    public int insert(T t) {

        return getEntityDao().insert(t);
    }

    @Override
    public T getById(Serializable id) {
        return getEntityDao().getById(id);
    }

    @Override
    public int update(T t) {
        return getEntityDao().update(t);
    }

    @Override
    public int delete(Serializable id) {
        return getEntityDao().delete(id);
    }

    @Override
    public PageInfo<T> findPage(Map<String, Object> filters) {
        //启动分页插件
        int pageNum = CastUtil.castInt(filters.get("pageNum"));
        int pageSize = CastUtil.castInt(filters.get("pageSize"));
        PageHelper.startPage(pageNum,pageSize);
        //查询数据库数据获取分页查询的结果
        Page<T> page = getEntityDao().findPage(filters);
        //Page转换为PageInfo并返回   就是PageInfo通过传入的Page进行更多的包装跟数据处理
        return new PageInfo<>(page.getResult(),10);
    }
}
